import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

import contants.GContants;
import contants.GContants.POLYGON_STATE;

public class GDrawingPanel extends JPanel {
	// attributes
	private POLYGON_STATE polygonState;
	private Polygon polygon;

	// aggregatinos
	private MouseInputHandler mouseInputHandler;
	
	// associations
	private GToolBar toolBar;
	public GToolBar getToolBar() {
		return toolBar;
	}
	public void setToolBar(GToolBar toolBar) {
		this.toolBar = toolBar;
	}

	
	public GDrawingPanel() {
		mouseInputHandler = new MouseInputHandler();
		this.addMouseListener(mouseInputHandler);
		this.addMouseMotionListener(mouseInputHandler);

		// temp
		polygonState = POLYGON_STATE.FIRST;

	}

	public void init(GToolBar toolBar) {
		this.toolBar = toolBar;

	}


	private void keepDrawing(Point startPoint, Point oldPoint,
			Point currentPoint) {

		Graphics2D g2d = (Graphics2D) getGraphics();
		g2d.setXORMode(this.getBackground());

		if (toolBar.getShapeType() == GContants.SHAPE_RECTANGLE) {
			g2d.drawRect(startPoint.x, startPoint.y, oldPoint.x - startPoint.x,
					oldPoint.y - startPoint.y);
			g2d.drawRect(startPoint.x, startPoint.y, currentPoint.x
					- startPoint.x, currentPoint.y - startPoint.y);
		} else if (toolBar.getShapeType() == GContants.SHAPE_CIRCLE) {
			g2d.drawOval(startPoint.x, startPoint.y, oldPoint.x - startPoint.x,
					oldPoint.y - startPoint.y);
			g2d.drawOval(startPoint.x, startPoint.y, currentPoint.x
					- startPoint.x, currentPoint.y - startPoint.y);
		} else if (toolBar.getShapeType() == GContants.SHAPE_LINE) {
			g2d.drawLine(startPoint.x, startPoint.y, oldPoint.x, oldPoint.y);
			g2d.drawLine(startPoint.x, startPoint.y, currentPoint.x,currentPoint.y);
		}
	}

	private void polygonAdd(Point currentPoint) {
		if (polygonState == POLYGON_STATE.FIRST) {
			polygon = new Polygon();
			polygonState = POLYGON_STATE.KEEP;
			polygon.addPoint(currentPoint.x, currentPoint.y);
		}
		polygon.addPoint(currentPoint.x, currentPoint.y);
	}

	private void polygonKeep(Point currentPoint) {
		Graphics2D g2d = (Graphics2D) getGraphics();
		g2d.setXORMode(this.getBackground());
		g2d.draw(polygon);
		polygon.xpoints[polygon.npoints - 1] = currentPoint.x;
		polygon.ypoints[polygon.npoints - 1] = currentPoint.y;
		g2d.draw(polygon);
	}

	private void polygonFinish(Point currentPoint) {
		polygonState = POLYGON_STATE.FIRST;
	}

	private class MouseInputHandler implements MouseInputListener {
		private Point startPoint, oldPoint, currentPoint;

		public void mousePressed(MouseEvent e) {
			startPoint = e.getPoint();
			oldPoint = startPoint;
			currentPoint = startPoint;
		}

		public void mouseClicked(MouseEvent e) {
			if (toolBar.getShapeType() == GContants.SHAPE_POLYGON) {
				if (e.getClickCount() == 1) {
					polygonAdd(e.getPoint());
				} else if (e.getClickCount() == 2) {
					polygonFinish(e.getPoint());
				}
			}
		}

		public void mouseEntered(MouseEvent e) {

		}

		public void mouseExited(MouseEvent e) {

		}

		public void mouseReleased(MouseEvent e) {

		}

		public void mouseDragged(MouseEvent e) {
			currentPoint = e.getPoint();
			keepDrawing(startPoint, oldPoint, currentPoint);
			oldPoint = currentPoint;
		}

		public void mouseMoved(MouseEvent e) {
			if (polygonState == POLYGON_STATE.KEEP) {
				polygonKeep(e.getPoint());
			}
		}
	}
}
